package com.situ.blog.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * localhost:8080/page/blog/list
 * localhost:8080/page/blog/add
 * localhost:8080/page/blog/update
 * localhost:8080/page/admin/list
 * localhost:8080/page/admin/add
 * localhost:8080/page/admin/update
 */
@WebServlet("/page/*")
public class PageServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.getRequestDispatcher("/WEB-INF/blog_list.jsp").forward(req, resp);
        //req.getRequestDispatcher("/WEB-INF/blog_add.jsp").forward(req, resp);
        //req.getRequestDispatcher("/WEB-INF/admin_list.jsp").forward(req, resp);
        String requestURI = req.getRequestURI();
        System.out.println("requestURI: " + requestURI);
        // /page/blog/list   /page/blog/add
        // /page/login
        String[] paths = requestURI.split("/");
        //["", "page", "blog", "list"]
        //["", "page", "login"]
        if (paths.length == 4) {
            req.getRequestDispatcher("/WEB-INF/"+paths[2]+"_"+paths[3]+".jsp").forward(req, resp);
        } else if (paths.length == 3) {
            req.getRequestDispatcher("/WEB-INF/"+paths[2]+".jsp").forward(req, resp);
        }
    }
}
